/*
    Copyright (C) Davor Jadrijevic
    
    See https://github.com/ChristopheJacquet/PiFmRds
    
    rds_wav.c is a test program that generates RDS
    bit message in VHDL format.

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/


#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>

#include "options.h" // autogenerated with gengetopt
#include "rds.h"

struct gengetopt_args_info options_info;
struct gengetopt_args_info *options = &options_info;

/* Simple test program */
int main(int argc, char **argv) {
    static uint8_t bit_buffer[BITS_PER_GROUP/8];
    uint16_t pi;
    char *ps, *rt;
    int ngroups = 4; // default 4 groups of 13 bytes, display only PS

    cmdline_parser(argc, argv, options);
    pi = options->pi_arg;
    ps = options->ps_arg; // set default first
    // override default with cmdline
    for (int i = 0; i < options->inputs_num ; i++)
      ps = options->inputs[i];
    // override cmdline option if explicitely specified
    if(options->ps_given)
      ps = options->ps_arg;
    if(options->rt_given)
      ngroups += 16;  // additional number of groups for RT
    rt = options->rt_arg;
    set_rds_pi(pi);
    set_rds_ps(ps);
    set_rds_rt(rt);
    rds_params.stereo = options->stereo_flag;
    rds_params.ta = options->ta_flag;
    for(int i, n=0; i < options->af_given; i++)
    {
      if(options->af_arg[i] > 87.4999 && options->af_arg[i] < 107.9001)
      {
        rds_params.af[n] = (int16_t)(options->af_arg[i] * 10.0 + 0.5);
        n++;
        rds_params.afs = n;
      }
    }
    printf("-- automatically generated with rds_msg\n");
    printf("library ieee;\n");
    printf("use ieee.std_logic_1164.all;\n");
    printf("use ieee.std_logic_arith.all;\n");
    printf("use ieee.std_logic_unsigned.all;\n");
    printf("use ieee.numeric_std.all;\n");
    printf("package message is\n");
    printf("type rds_msg_type is array(0 to %d) of std_logic_vector(7 downto 0);\n", ngroups*13-1);
    printf("-- PI=0x%04X\n", pi);
    printf("-- STEREO=%s\n", options->stereo_given ? "Yes" : "No");
    printf("-- TA=%s\n", options->ta_given ? "Yes" : "No");
    if(rds_params.afs>0)
    {
      printf("-- AF=");
      for(int i = 0; i < rds_params.afs; i++)
        printf("%.1f ", rds_params.af[i]*0.1);
      printf("MHz\n");
    }
    else
      printf("-- AF=No\n");
    printf("-- PS=\"%s\"\n", ps);
    if(options->rt_given)
      printf("-- RT=\"%s\"\n", rt);
    printf("constant rds_msg_map: rds_msg_type := (\n");
    for(int i = 0; i < 4; i++)
    {
      write_ps_group(bit_buffer, i);
      for(int j = 0; j < BITS_PER_GROUP/8; j++)
        printf("x\"%02x\",", bit_buffer[j]);
      printf("\n");
    }
    if(options->rt_given)
    {
      for(int i = 0; i < 16; i++)
      {
        write_rt_group(bit_buffer, i);
        for(int j = 0; j < BITS_PER_GROUP/8; j++)
          printf("x\"%02x\",", bit_buffer[j]);
        printf("\n");
      }
    }
    printf("others => (others => '0')\n");
    printf(");\n");
    printf("end message;\n");
    return EXIT_SUCCESS;
}
